//制造traffic数据
var List = [];
import Mock from "mockjs";
import { param2Obj } from "../untial/urlUtils";

for (var i = 0; i < 100; i++) {
  List.push({
    id: Mock.mock("@integer()"),
    name: Mock.mock("@cname"),
    personnelnum: Mock.mock("@zip"),
    role: Mock.mock("@integer(0, 1)"), //0是管理者 1是街道负责人
    explain: Mock.mock("@integer(0, 1)"), //0是该账号拥有权限 1是上传车位信息
    newdate: Mock.mock("@datetime"),
    type: Mock.mock("@integer(0, 1)"), //0 是 1 否
  });
}
export default {
  getTrafficData: (config) => {
    console.log("config", param2Obj(config.url));
    var { keywordname, keywordnum, page, pageSize } = param2Obj(config.url);
    var res = List.filter((item) => item.name.indexOf(keywordname) != -1);
    var res1 = res.filter(
      (item) => item.personnelnum.indexOf(keywordnum) != -1
    );
    // (page-1)*pageSize<=index<page*pageSize
    var resLimit = res1.filter((item, index) => {
      return (page - 1) * pageSize <= index && index < page * pageSize;
    });

    return {
      code: 200,
      msg: "请求成功",
      data: resLimit,
      total: List.length,
    };
  },

  deleteTrafficData: (config) => {
    console.log("删除的config", config);
    // 获取删除数据的id
    var { id } = JSON.parse(config.body);
    var res = List.filter((item) => item.id != id);
    List = res;
    return {
      code: 200,
      msg: "删除成功",
      data: null,
    };
  },

  addTrafficData: (config) => {
    console.log("添加的config", config);
    const { name, role, type } = JSON.parse(config.body);
    List.unshift({
      id: Mock.mock("@integer()"),
      name: name,
      personnelnum: Mock.mock("@zip"),
      role: role,
      explain: Mock.mock("@integer(0, 1)"),
      newdate: Mock.mock("@datetime"),
      type: type,
    });
    return {
      code: 200,
      msg: "添加成功",
      data: null,
    };
  },

  editTrafficData: (config) => {
    const { id, name, role, type } = JSON.parse(config.body);
    List.forEach((item) => {
      if (item.id == id) {
        item.name = name;
        item.role = role;
        item.type = type;
        return;
      }
    });
    return {
      code: 200,
      msg: "修改成功",
      data: null,
    };
  },
};
